<?php
class Edit extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/node/'));
		
		$roleId = $this->get('id', 'int');
		if(!$roleId) header('Location:/rbac/role/');
		
		$data = array();
		$tpl = 'rbac/role/roleinfo.html';
		
		$mRole = new Role_model();
		//获取分组信息
		$data['info'] = $mRole->roleById($roleId);
			
		$data['act'] = 'edit';
		$this->display($data, $tpl);
	}
	public function json() {
		$roleId = $this->post('roleId', 'int');
		$name = $this->post('name', 'str');
		
		if(!$roleId) $this->displayJSON(array('status' => -1, 'message' => '未知角色ID'));
		if(!$name) $this->displayJSON(array('status' => -2, 'message' => '请输入角色名称'));
		
		$mRole = new Role_model();
		//检查分组
		$params = array('role_id !=' => $roleId, 'name' => $name);
		$result = $mRole->roles($params);
		if($result) $this->displayJSON(array('status' => -3, 'message' => '角色已存在'));
		
		$cols = array('name' => $name);
		$result = $mRole->editRoleById($roleId, $cols);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '修改角色成功'));
		else $this->displayJSON(array('status' => -4, 'message' => '修改角色失败'));
	}
}